package com.bwstudio.common.autoidempotent.config;

import com.bwstudio.common.autoidempotent.interceptor.AutoIdempotentInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.annotation.Resource;

/**
 * @author buwei
 */
@Slf4j
@Configuration
@ComponentScan("com.bwstudio.common.autoidempotent")
@EnableConfigurationProperties(AutoIdempotentProperties.class)
@ConditionalOnProperty(prefix = "redis-auto-idempotent", value = "enabled", havingValue = "true")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AutoIdempotentWebConfiguration implements WebMvcConfigurer {

    private final AutoIdempotentProperties autoIdempotentProperties;

    private final AutoIdempotentInterceptor autoIdempotentInterceptor;

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        log.info("---------- 自动幂等拦截器【启用】 ----------");

        Long tokenExpireTimeInMs = autoIdempotentProperties.getTokenExpireTimeInMs();
//		String apiUri = "/**";
        log.info("---------- 幂等性token失效时间（ms）：" + tokenExpireTimeInMs);

        //自动幂等控制器拦截
        registry.addInterceptor(autoIdempotentInterceptor);
    }
}
